<?php

class Emms_Form_Admin_Bank extends HTech_Form
{
    var $_primary  = 'BankId';
    
    private $_clientOptions = array();
    private $_officerOptions = array();
    private $_communityOptions = array();
    private $_defaultOfficer;
    private $_defaultCommunity;
    
    public function __construct() 
    {
        parent::__construct(); 
        $this->_setOptions();
        
        $this->setName('Bank');
        
        $this->setAttrib('title', $this->translate("Bank form"));
        $this->setDescription($this->translate("Edit bank information"));
        
        $bankId = new Zend_Form_Element_Hidden('BankId');
        
        $name = new Zend_Form_Element_Text('Name');
        $name->setLabel("Name")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addFilter('Alpha', array('allowwhitespace' => true))
            ->addValidator('StringLength',false,
                array(
                    'min'      => 2,
                    'max'      => 128,
                    'messages' => $this->translate("From %min% to %max% characters only")
                ));
        
        $officerUserId = new Zend_Form_Element_Select('OfficerUserId');
        $officerUserId->setLabel("Loan officer") 
            ->setRequired(true)
            ->addValidator('GreaterThan', true, array('min' => 0))
            ->addMultiOptions($this->_officerOptions)
            ->addErrorMessage($this->translate("Required field, can't be empty"));
        
        $communityId = new Zend_Form_Element_Select('CommunityId');
        $communityId->setLabel("Community")  
            ->setRequired(true)               
            ->setAttrib('size','5')
            ->addValidator('GreaterThan', true, array('min' => 0))
            ->addMultiOptions($this->_communityOptions)
            ->addErrorMessage($this->translate("Required field, can't be empty")); 
                                
        $presidentClientId = new Zend_Form_Element_Select('PresidentClientId');
        $presidentClientId->setLabel("President")
            ->addMultiOptions($this->_clientOptions); 
                                
        $treasurerClientId = new Zend_Form_Element_Select('TreasurerClientId');
        $treasurerClientId->setLabel("Treasurer")
            ->addMultiOptions($this->_clientOptions);    
                        
        $secretaryClientId = new Zend_Form_Element_Select('SecretaryClientId');
        $secretaryClientId->setLabel("Secretary")
            ->addMultiOptions($this->_clientOptions);  
                        
        $comments = new Zend_Form_Element_Textarea('Comments');
        $comments->setLabel("Comments")
            ->setAttrib('rows', 5); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->setDecorators(array(array('ViewScript', array('viewScript' => 'bank/_form_add.phtml'))));
        $this->addElements(array($name,$communityId,$officerUserId,$presidentClientId,
            $treasurerClientId,$secretaryClientId,$comments,$submit,$bankId));
        
        $this->setDefaults(array('OfficerUserId' => $this->_defaultOfficer, 'CommunityId' => $this->_defaultCommunity)); 
    }
    
    protected function _setOptions()
    {
        if($this->_request->isPost()) {
            $param = $this->_request->getPost(); 
            if(!isset($param['OfficerUserId'])) $param['OfficerUserId'] = 0;
            if(!isset($param['CommunityId'])) $param['CommunityId'] = 0;
            if(!isset($param['BankId'])) $param['BankId'] = 0;
            $this->_defaultOfficer = (int) $param['OfficerUserId']; 
            $this->_defaultCommunity = (int) $param['CommunityId']; 
            $this->_officerOptions = Officers::getOfficerOptions('Bank', (int) $param['BankId']);
            $this->_communityOptions = Communities::getCommunityOptions('Officer', (int) $param['OfficerUserId']);
            $this->_clientOptions = array('-') + Clients::getClientOptions('Bank', (int) $param['BankId']);
        } else {
            $param = $this->_request->getParams();
            if(!isset($param['id'])) $param['id'] = 0;
            if(!isset($param['officer'])) $param['officer'] = 0;
            if((int) $param['id'] > 0) {
                $this->_setOptionsByBank((int) $param['id']);
            } elseif ((int) $param['officer'] > 0) {
                $this->_setOptionsByOfficer((int) $param['officer']);
            } else {
                $this->_officerOptions = Officers::getOfficerOptions();
            }
        }
    }
    
    private function _setOptionsByBank($id)
    {
        $banks = new Banks();
        try {
            $bank = $banks->getRecord($id);
        } catch (Exception $e) {
            $this->deny($e);
        }
        $this->_defaultOfficer = $bank->d->OfficerUserId;     
        $this->_defaultCommunity = $bank->d->CommunityId;
        
        $this->_officerOptions = Officers::getOfficerOptions('Bank', $id);
        $this->_communityOptions = Communities::getCommunityOptions('Bank', $id); 
        $this->_clientOptions = array('-') + Clients::getClientOptions('Bank', $id);
    }
    
    private function _setOptionsByOfficer($id)
    {
        $officers = new Officers();
        try {
            $officer = $officers->getRecord($id);
        } catch (Exception $e) {
            $this->deny($e);
        }
        $this->_defaultOfficer = $officer->d->OfficerUserId; 
        $this->_defaultCommunity = 0; 
        
        $this->_officerOptions = Officers::getOfficerOptions();
        $this->_communityOptions = Communities::getCommunityOptions('Officer', $officer->d->OfficerUserId);
    }
}